package com.xm.xmapp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.adapter.RollViewPager;
import com.xm.xmapp.adapter.RollViewPager.OnPagerClickCallback;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.LongRentDateTimePickDialog;
import com.xm.xmapp.customview.TipDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Cancel_longrent_subscribeRequest;
import com.xm.xmapp.request.bean.Submit_longrent_subscribeRequest;
import com.xm.xmapp.response.bean.Get_network_car_genre_listResponse;
import com.xm.xmapp.response.bean.LongRentMoneyBean;
import com.xm.xmapp.response.bean.LongRentSubscribeBean;
import com.xm.xmapp.response.bean.ResponseLongRentSubscribe;
import com.xm.xmapp.response.bean.UserTimeBean;
import com.xm.xmapp.tools.ScreenUtils;
import com.xm.xmapp.tools.Tools;

public class LongRentOrderYuyueActivity extends BaseActivity implements OnClickListener {

	private ImageView left_button, right_button, back, phone;
	private LinearLayout scene_ad;
	private TextView title;
	private String network_id, network_name;
	private int page = 0;
	private LinearLayout lin1, lin2;
	private ArrayList<String> urlList = new ArrayList<String>();
	// 图片轮播pager
	private RollViewPager viewPager;
	// private ArrayList<View> dotList = new ArrayList<View>();
	private Get_network_car_genre_listResponse bean;
	private LongRentSubscribeBean bean1;
	/**
	 * 支付订金以后，只能取消预约，其它按钮都不能点
	 */
	private boolean isOnclick = true;
	private RelativeLayout userTimeRel;
	private TextView chexing, time, userTime, name, yajin, userTime2, userMoney, yuYajin, dingjin, right_title;
	private UserTimeBean userTimeBean;
	private LongRentDateTimePickDialog dateTimePickDialog;
	private EditText salesman_code;
	private Submit_longrent_subscribeRequest requestBean;
	private LongRentMoneyBean longRentMoneyBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_longrent_order_subscribe);
	}

	@Override
	public void initView() {

		left_button = (ImageView) findViewById(R.id.left_button);
		right_button = (ImageView) findViewById(R.id.right_button);
		back = (ImageView) findViewById(R.id.back);
		title = (TextView) findViewById(R.id.title);
		chexing = (TextView) findViewById(R.id.chexing);
		phone = (ImageView) findViewById(R.id.phone);
		time = (TextView) findViewById(R.id.time);
		userTime = (TextView) findViewById(R.id.userTime);
		name = (TextView) findViewById(R.id.name);
		yajin = (TextView) findViewById(R.id.yajin);
		userTime2 = (TextView) findViewById(R.id.userTime2);
		userMoney = (TextView) findViewById(R.id.userMoney);
		yuYajin = (TextView) findViewById(R.id.yuYajin);
		dingjin = (TextView) findViewById(R.id.dingjin);
		right_title = (TextView) findViewById(R.id.right_title);
		lin1 = (LinearLayout) findViewById(R.id.lin1);
		lin2 = (LinearLayout) findViewById(R.id.lin2);
		scene_ad = (LinearLayout) findViewById(R.id.scene_ad);
		// dotLl = (LinearLayout) findViewById(R.id.dots_ll);
		userTimeRel = (RelativeLayout) findViewById(R.id.userTimeRel);
		salesman_code = (EditText) findViewById(R.id.salesman_code);
	}

	@Override
	public void initData() {

		right_title.setText("了解更多");

		LayoutParams laParams = (LayoutParams) scene_ad.getLayoutParams();
		laParams.width = ScreenUtils.getScreenWidth(this) * 7 / 11;
		laParams.height = laParams.width * 2 / 3;
		scene_ad.setLayoutParams(laParams);
		longRentMoneyBean = new LongRentMoneyBean();
		title.setText("长时预约");
		String str = getIntent().getStringExtra("bean");
		bean = JSON.parseObject(str, Get_network_car_genre_listResponse.class);
		String JSONString = getIntent().getStringExtra("JSONString");
		if (JSONString != null && !JSONString.equals("")) {
			bean1 = JSON.parseObject(JSONString, LongRentSubscribeBean.class);
			network_id = bean1.getSubscribe_network_id();
			network_name = bean1.getNetwork_name();
		} else {
			network_id = getIntent().getStringExtra("network_id");
			network_name = getIntent().getStringExtra("network_name");
		}
		name.setText(network_name);
		dateTimePickDialog = new LongRentDateTimePickDialog(this, new MyDialogButton() {

			@Override
			public void setTitle(int flag, String str) {
				time.setText(str);
				requestBean.take_date = str;
			}
		});

		requestBean = new Submit_longrent_subscribeRequest();
		requestBean.network_id = network_id;
		requestBean.salesman_code = salesman_code.getText().toString();
		if (bean1 != null) {
			lin2.setVisibility(View.VISIBLE);
			requestBean.subscribe_id = bean1.subscribe_id;
			requestBean.salesman_code = bean1.salesman_code;
			salesman_code.setText(bean1.salesman_code + "");
			if (bean1.pay_finish_time != 0) {
				isOnclick = false;
				userTime.setCompoundDrawables(null, null, null, null);
				lin1.setVisibility(View.GONE);
				findViewById(R.id.rel).setVisibility(View.VISIBLE);
				dingjin.setText(bean1.pay_money + "元");

				if (bean1.salesman_code.equals("0")) {
					findViewById(R.id.lin).setVisibility(View.GONE);
				} else {
					salesman_code.setEnabled(false);
				}

			}

		} else {
			lin2.setVisibility(View.GONE);
		}
		getData();
		if (bean1 != null) {
			if (userTimeBean == null)
				userTimeBean = new UserTimeBean();
			userTimeBean.time = bean1.tenancy_type;
			userTimeBean.name = bean1.tenancy_type + "个月";
			if (userTimeBean.time == 6)
				userTimeBean.name = "半年";
			if (userTimeBean.time == 12)
				userTimeBean.name = "一年";

		} else {
			if (userTimeBean == null)
				userTimeBean = new UserTimeBean();
			userTimeBean.time = 1;
			userTimeBean.name = "1个月";

		}
		initImage();

		if (bean1 != null) {
			yajin.setText(bean1.deposit_money + "元");
			longRentMoneyBean.yaJing = bean1.deposit_money;

			userMoney.setText(bean1.rentcar_money + "元");
			longRentMoneyBean.zuJin = bean1.rentcar_money;
		}

		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		if (bean1 != null) {
			try {
				Date date1 = matter1.parse(bean1.take_date);
				Date date2 = matter1.parse(matter1.format(dt));

				if (date1.getTime() < date2.getTime()) {
					time.setText(matter1.format(dt));
					requestBean.take_date = matter1.format(dt);
					dateTimePickDialog.setMaxCalendar(bean.carGenreList.get(page).car_longrent_subscribe_days);
					time.setText(bean1.take_date);
					dateTimePickDialog.setInitDateTime(bean1.take_date);
					Tools.showMessage("取车时间已过");
				} else {

					requestBean.take_date = bean1.take_date;
					time.setText(bean1.take_date);
					dateTimePickDialog.setInitDateTime(bean1.take_date);
					dateTimePickDialog.setMaxCalendar(bean.carGenreList.get(page).car_longrent_subscribe_days);

				}

			} catch (Exception e) {

			}

		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dt);
			calendar.add(Calendar.DAY_OF_MONTH, +1);// +1今天的时间加一天
			dt = calendar.getTime();
			time.setText(matter1.format(dt));
			requestBean.take_date = matter1.format(dt);
			dateTimePickDialog.setMaxCalendar(bean.carGenreList.get(page).car_longrent_subscribe_days);
		}
	}

	private void getData() {

		if (bean.carGenreList == null || bean.carGenreList.size() == 0) {
			Tools.showMessage("该网点暂无车型");
			finish();
		} else {
			for (int i = 0, size = bean.carGenreList.size(); i < size; i++) {
				urlList.add(bean.carGenreList.get(i).car_genre_url);
				if (bean1 != null && bean1.car_genre_id.equals(bean.carGenreList.get(i).car_genre_id)) {
					page = i;
				}
			}
			viewPager = new RollViewPager(LongRentOrderYuyueActivity.this, null, R.mipmap.dot_focus,
					R.mipmap.dot_normal, new OnPagerClickCallback() {
						@Override
						public void onPagerClick(int position) {

						}
					});
			viewPager.setUriList(urlList);
			viewPager.startRoll();
			scene_ad.removeAllViews();
			scene_ad.addView(viewPager);

			viewPager.setCurrentItem(page);
			viewPager.setNoScroll(!isOnclick);
			if (isOnclick) {
				left_button.setVisibility(View.VISIBLE);
				right_button.setVisibility(View.VISIBLE);
				viewPager.addOnPageChangeListener(new OnPageChangeListener() {

					@Override
					public void onPageSelected(int arg0) {
						page = arg0;
						initImage();
					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {

					}
				});
			} else {
				left_button.setVisibility(View.INVISIBLE);
				right_button.setVisibility(View.INVISIBLE);
			}
		}

	}

	private void initImage() {

		if (page == 0) {
			left_button.setImageResource(R.mipmap.left);
		} else {
			left_button.setImageResource(R.mipmap.left1);
		}

		if (page == (urlList.size() - 1) || urlList.size() == 0) {
			right_button.setImageResource(R.mipmap.right);
		} else {
			right_button.setImageResource(R.mipmap.right1);
		}

		if (bean != null && bean.carGenreList != null && bean.carGenreList.size() > 0) {
			requestBean.car_genre_id = bean.carGenreList.get(page).car_genre_id;
			if (userTimeBean != null) {
				requestBean.tenancy_type = userTimeBean.time;
			}

			chexing.setText(bean.carGenreList.get(page).car_genre_name);
			if (userTimeBean == null) {
				yajin.setText(bean.carGenreList.get(page).deposit_money_array.getString("1") + "元");
				longRentMoneyBean.yaJing = bean.carGenreList.get(page).deposit_money_array.getString("1");
			} else {
				yajin.setText(bean.carGenreList.get(page).deposit_money_array.getString(userTimeBean.time + "") + "元");
				longRentMoneyBean.yaJing = bean.carGenreList.get(page).deposit_money_array.getString(userTimeBean.time
						+ "");
			}

			if (userTimeBean != null) {
				userTime.setText(userTimeBean.name);
				userTime2.setText(userTimeBean.name);
			}

			if (userTimeBean == null) {
				userMoney.setText(bean.carGenreList.get(page).rentcar_money_array.getString("1") + "元");
				longRentMoneyBean.zuJin = bean.carGenreList.get(page).rentcar_money_array.getString("1");
			} else {
				userMoney.setText(bean.carGenreList.get(page).rentcar_money_array.getString(userTimeBean.time + "")
						+ "元");
				longRentMoneyBean.zuJin = bean.carGenreList.get(page).rentcar_money_array.getString(userTimeBean.time
						+ "");
			}
			yuYajin.setText("确定支付" + bean.carGenreList.get(page).longrent_subscribe_deposit + "元订金");
			longRentMoneyBean.diJin = bean.carGenreList.get(page).longrent_subscribe_deposit;

		}

	}

	@Override
	public void initEvent() {

		back.setOnClickListener(this);
		left_button.setOnClickListener(this);
		right_button.setOnClickListener(this);
		lin1.setOnClickListener(this);
		userTimeRel.setOnClickListener(this);
		time.setOnClickListener(this);
		lin2.setOnClickListener(this);
		phone.setOnClickListener(this);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		switch (resultCode) {
		case CodeConstant.ZushiCode:
			userTimeBean = (UserTimeBean) data.getSerializableExtra("bean");
			initImage();
			break;

		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.phone:
			TipDialog tipDialog1 = new TipDialog(this, "温馨提示", CodeConstant.phone, new MyDialogButton() {

				@Override
				public void setTitle(int flag, String str) {

					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + CodeConstant.phone));
					startActivity(intent);
				}
			});
			tipDialog1.show();
			tipDialog1.setRightText("呼叫");
			if (tipDialog1.getView() != null) {
				tipDialog1.getView().setVisibility(View.GONE);
			}
			break;
		case R.id.lin2:

			String str = "";
			if (isOnclick) {
				str = "确定取消预约吗？";
			} else {
				str = "取消预约后，您支付的订金" + bean1.pay_money + "元将会退回到您的车辆押金中，确定要取消吗？";
			}
			TipDialog tipDialog = new TipDialog(this, "温馨提示", str, new MyDialogButton() {

				@Override
				public void setTitle(int flag, String str) {
					cancel_longrent_subscribeRequest();
				}
			});
			tipDialog.show();
			break;

		case R.id.lin1:
			if (isOnclick)
				submit_longrent_subscribeRequest();

			break;
		case R.id.back:
			finish();
			break;

		case R.id.userTimeRel:
			if (isOnclick) {
				intent = new Intent(MyAppcation.getMyAppcation(), UseTimeActivity.class);
				startActivityForResult(intent, CodeConstant.ZushiCode);
			}
			break;
		case R.id.time:
			if (isOnclick) {
				dateTimePickDialog.show();
				dateTimePickDialog.setStrTitle("最长预约取车天数:" + bean.carGenreList.get(page).car_longrent_subscribe_days
						+ "天");
			}

			break;

		case R.id.right_button:
			if (isOnclick) {
				if (urlList != null && urlList.size() > 0 && page != (urlList.size() - 1)) {
					page++;
					viewPager.setCurrentItem(page);
				}
			}
			break;

		case R.id.left_button:
			if (isOnclick) {
				if (page != 0) {
					page--;
					viewPager.setCurrentItem(page);
				}
			}
			break;

		default:
			break;
		}
	}

	private void cancel_longrent_subscribeRequest() {

		Cancel_longrent_subscribeRequest requestBean = new Cancel_longrent_subscribeRequest();
		requestBean.subscribe_id = bean1.subscribe_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class,
						Type.A_Main.LONGRENT_ORDER_CHANGE, null);
				finish();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void submit_longrent_subscribeRequest() {
		requestBean.salesman_code = salesman_code.getText().toString();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				ResponseLongRentSubscribe bean1 = JSON.parseObject(response, ResponseLongRentSubscribe.class);
				intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderPayActivity.class);
				intent.putExtra("order_id", bean1.subscribe_id);
				intent.putExtra("longRentMoney", longRentMoneyBean.diJin);
				startActivity(intent);
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class,
						Type.A_Main.LONGRENT_ORDER_CHANGE, null);
				finish();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

}
